<template>
  <div>
    <van-popup
      v-model="visible"
      position="bottom"
      :style="{ height: 'auto' }"
      @close="handleCancel">
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <div style="margin-top:10px;width:100%;">
            <a-form-item label="客户名称">
              <a-input placeholder="请输入客户名称" v-model="queryParam.workerName"></a-input>
            </a-form-item>
          </div>
          <a-row>
            <a-col :span="24">
              <div style="margin-bottom:5px;">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a-button type="primary" @click="handleOk" style="margin-left: 8px">确定</a-button>
              </div>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div>
        <van-radio-group v-model="radio" class="list">
          <van-list
            v-model="loading"
            :finished="finished"
            offset="30"
            finished-text="没有更多了"
            @load="downLoadData"
            :immediate-check="false">
            <van-radio
              style="padding: 0 20px;"
              v-for="(item,i) in dataSource"
              :key="i"
              :name="i">
              <div class="userItem">
                <span style="width:20%;">{{ item.workerName }}</span>
                <span style="width:40%;">{{ item.workerPhone }}</span>
              </div>
            </van-radio>
          </van-list>
        </van-radio-group>
      </div>
    </van-popup>
  </div>
  <!--<a-modal
    centered
    :title="title"
    :width="1000"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    &lt;!&ndash; 查询区域 &ndash;&gt;
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :span="8">
            <a-form-item label="工人姓名">
              <a-input placeholder="请输入工人姓名" v-model="queryParam.workerName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="工人种类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplier">
              <a-select
                show-search
                :allowClear="true"
                placeholder="请选择工种"
                option-filter-prop="children"
                v-model="queryParam.workerType"
                style="width: 100%"
                :filter-option="filterOptionKindOfWork"
                @change="changeKindOfWork">
                <a-select-option :value="item.id" v-for="(item,index) in kindOfWorkData" :key="index">
                  {{item.kindOfWork}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
                    <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                      <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                      <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                    </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    &lt;!&ndash; table区域-begin &ndash;&gt;
    <div>
      <a-table
        size="small"
        bordered
        rowKey="id"
        :columns="columns1"
        :dataSource="dataSource1"
        :pagination="ipagination"
        :loading="loading"
        :scroll="{ y: 240 }"
        :rowSelection="{selectedRowKeys: selectedRowKeys,onSelectAll:onSelectAll,onSelect:onSelect,onChange: onSelectChange,type:'radio'}"
        @change="handleTableChange">

      </a-table>
    </div>
    &lt;!&ndash; table区域-end &ndash;&gt;


  </a-modal>-->
</template>

<script>

  import {filterObj} from '@/utils/util'
  import {getAction} from '@/api/manage'
  import {filterDictTextByCache} from '@comp/dict/JDictSelectUtil'

  export default {
    name: "appointWorkerModal",
    data() {
      return {
        title: "",
        names: [],
        visible: false,
        placement: 'right',
        description: '',
        // 查询条件
        queryParam: {},
        //职务条件
        post:'',
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        // 表头
        columns1: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '工人姓名',
            align: "center",
            width: 100,
            dataIndex: 'workerName'
          },
          {
            title: '性别',
            align: "center",
            width: 100,
            dataIndex: 'workerSex',
            customRender: (text) => {
              //字典值翻译通用方法
              return filterDictTextByCache('sex', text);
            }
          },
          {
            title: '电话',
            align: "center",
            width: 100,
            dataIndex: 'workerPhone'
          },
          {
            title: '工种',
            align: "center",
            width: 150,
            dataIndex: 'workerTypeName'
          }
        ],
        //数据集
        dataSource: [],
        //工种数据
        kindOfWorkData:[],
        // 分页参数
        ipagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + "-" + range[1] + " 共" + total + "条"
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        isorter: {
          column: 'createTime',
          order: 'desc',
        },
        loading: false,
        selectedRowKeys: [],
        selectedRows: [],
        url: {
          list: "/GhWorkerInfo/list",
          kindOfWorkList: "/ghKindOfWork/queryList"
        },
        radio: '',
        finished:false,
      }
    },
    created() {
      this.loadData();
      this.initKindOfWork();
    },
    methods: {
      filterOptionKindOfWork(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      changeKindOfWork(value){
        this.loadData(1);
      },
      searchQuery() {
        this.loadData(1);
      },
      searchReset() {
        this.queryParam = {};
        this.loadData(1);
      },
      handleCancel() {
        this.visible = false;
      },
      handleOk() {
        if (this.radio === ''){
          this.$message.warning("请选择一个用户")
          return;
        }
        this.selectedRows = [
          this.dataSource[this.radio]
        ]
        this.$emit("selectFinished", this.selectedRows[0]);
        this.visible = false;
      },
      add() {
        this.visible = true;
      },
      downLoadData(){
        this.ipagination.current++;
        this.finished = false
        this.loadData()
      },
      loadData(arg) {
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
          this.dataSource = []
        }
        var params = this.getQueryParams();//查询条件
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = [...this.dataSource, ...res.result.records];
            this.ipagination.total = res.result.total;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            this.loading = false;
            if(res.result.records.length<10){
              this.finished = true;
            }
          }
        })
      },
      initKindOfWork(){
        getAction(this.url.kindOfWorkList).then((res) => {
          if (res.success) {
            this.kindOfWorkData = res.result.records || res.result;
          }
        })
      },
      getQueryParams() {
        var param = Object.assign({}, this.queryParam, this.isorter);
        param.field = this.getQueryField();
        param.pageNo = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
        param.post = this.post;
        return filterObj(param);
      },
      getQueryField() {
        //TODO 字段权限控制
      },
      onSelectAll(selected, selectedRows, changeRows) {
        /*if (selected === true) {
          for (var a = 0; a < changeRows.length; a++) {
            this.dataSource2.push(changeRows[a]);
          }
        } else {
          for (var b = 0; b < changeRows.length; b++) {
            this.dataSource2.splice(this.dataSource2.indexOf(changeRows[b]), 1);
          }
        }*/
      },
      onSelect(record, selected) {
        /*console.log("============"+record.workerName)*/
        /*if (selected === true) {
          this.dataSource2.push(record);
        } else {
          var index = this.dataSource2.indexOf(record);
          if (index >= 0) {
            this.dataSource2.splice(this.dataSource2.indexOf(record), 1);
          }
        }*/
      },
      onClearSelected() {
        this.selectedRowKeys = [];
        this.selectionRows = [];
      },
      onSelectChange(selectedRowKeys, selectionRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectionRows
      },

      handleTableChange(pagination, filters, sorter) {
        //分页、排序、筛选变化时触发
        //TODO 筛选
        if (Object.keys(sorter).length > 0) {
          this.isorter.column = sorter.field;
          this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
        }
        this.ipagination = pagination;
        this.loadData();
      }
    }
  }
</script>

<style scoped lang="less">
  .list{
    overflow-y: scroll;
    height:380px;
  .userItem{
    display:flex;
    justify-content:space-between;
    height: 40px;
    line-height: 40px;
    border-top: 1px solid #f6f6f6;
  span{
    text-align:left;
  }
  }
  }
  .list /deep/ .van-radio__label{
    width:calc(100% - 30px);
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .table-page-search-wrapper{
    padding: 0 20px;
  }
  .ant-card-body .table-operator {
    margin-bottom: 18px;
  }

  .ant-table-tbody .ant-table-row td {
    padding-top: 15px;
    padding-bottom: 15px;
  }

  .anty-row-operator button {
    margin: 0 5px
  }

  .ant-btn-danger {
    background-color: #ffffff
  }

  .ant-modal-cust-warp {
    height: 100%
  }

  .ant-modal-cust-warp .ant-modal-body {
    height: calc(100% - 110px) !important;
    overflow-y: auto
  }

  .ant-modal-cust-warp .ant-modal-content {
    height: 90% !important;
    overflow-y: hidden
  }
  .ant-form-item{
    margin-bottom:10px !important;
  }
  .table-page-search-submitButtons{
    margin-bottom:10px !important;
  }
</style>